package com.clip.dao.csch;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.clip.dao.BasicDao;
import com.clip.entity.csch.Department;
import com.clip.entity.csch.Department;
@Repository
public class DepartmentDAO extends BasicDao{
	public static final String NAME = "name";
	public static final String PID = "pid";
	
	public void save(Department transientInstance) {
        try {
            getSession().save(transientInstance);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
	public void delete(Department persistentInstance) {
        try {
            getSession().delete(persistentInstance);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    public Department findById( java.lang.Integer id) {
        try {
        	Department instance = (Department) getSession()
                    .get(Department.class, id);
            return instance;
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    
    public List findByExample(Department instance) {
        try {
            List results = getSession()
                    .createCriteria(Department.class)
                    .add(Example.create(instance))
            .list();
            return results;
        } catch (RuntimeException re) {
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      try {
         String queryString = "from Department as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         throw re;
      }
	}


	public List findAll() {
		try {
			String queryString = "from Department";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
    public Department merge(Department detachedInstance) {
        try {
        	Department result = (Department) getSession()
                    .merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public void attachDirty(Department instance) {
        try {
            getSession().saveOrUpdate(instance);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    public void attachClean(Department instance) {
        try {
            getSession().lock(instance, LockMode.NONE);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    public void excuteUpdateQuery(String query){
    	try {
			getSession().createQuery(query).executeUpdate();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public List findByQuery(String query){
		return getSession().createQuery(query).list();
    }
    
    public Object countByQuery(String query){
    	return getSession().createQuery(query).uniqueResult();
    }
}
